home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter08 / RotatePanel.java < prev    next >
Text File  |  2000-06-29  |  2KB  |  101 lines

  1. package applets;
  2.  
  3. import shout3d.*;
  4. import shout3d.core.*;
  5. import shout3d.math.*;
  6.  
  7.  
  8. public class RotatePanel extends Shout3DPanel implements DeviceObserver {
  9.     
  10.     //Node in scene to rotate
  11.     Transform boxTrans;
  12.  
  13.     //pixels on screen
  14.     int pixelStartX;
  15.     int pixelStartY;
  16.     int pixelEndX;
  17.     int pixelEndY;
  18.  
  19.  
  20.     //to store and compute angles
  21.     float [] eulers = new float [3];
  22.     float [] axisAngle = new float [4];
  23.     Quaternion q = new Quaternion();
  24.     
  25.  
  26.     public RotatePanel(Shout3DApplet applet){
  27.         super(applet);
  28.     }
  29.         
  30.         public void customInitialize() {
  31.         
  32.         addDeviceObserver(this,"MouseInput", null);
  33.         boxTrans = (Transform) getNodeByName("Box01");
  34.         
  35.         //get starting rotation value from scene
  36.         axisAngle = boxTrans.rotation.getValue();
  37.         
  38.         //put this rotation into the quaternion
  39.         q.setAxisAngle(axisAngle);
  40.         
  41.         //get it back out as eulers
  42.         q.getEulers(eulers);
  43.         
  44.     
  45.     }
  46.  
  47.  
  48.     protected void finalize()  { 
  49.         removeDeviceObserver(this,"MouseInput");
  50.     }
  51.  
  52.     
  53.     
  54.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  55.         MouseInput mi = (MouseInput) di;
  56.         switch (mi.which){
  57.  
  58.             case MouseInput.DOWN:
  59.                 pixelStartX = mi.x;
  60.                 pixelStartY = mi.y;
  61.                 return true;
  62.  
  63.             case MouseInput.DRAG:
  64.  
  65.                 pixelEndX = mi.x;
  66.                 pixelEndY = mi.y;
  67.  
  68.                 int dragDistanceX = pixelEndX - pixelStartX;
  69.                 int dragDistanceY = pixelEndY - pixelStartY;
  70.                 
  71.                 //convert drag to rotation
  72.                 //at 50 pixels per radian (57.2 degrees)
  73.                 float headingDelta = dragDistanceX/50f;
  74.                 float pitchDelta = dragDistanceY/50f;
  75.                 
  76.                 //compute new heading and pitch
  77.                 eulers[0] = eulers[0] + headingDelta;
  78.                 eulers[1] = eulers[1] + pitchDelta;
  79.  
  80.                 //Convert from Eulers to AxisAngle
  81.                 // using the Quaternion
  82.                 q.setEulers(eulers);
  83.                 q.getAxisAngle(axisAngle);
  84.  
  85.                 //set the Transform to
  86.                 //updated axis angle values
  87.                 boxTrans.rotation.setValue(axisAngle);
  88.  
  89.                 pixelStartX = pixelEndX;
  90.                 pixelStartY = pixelEndY;
  91.                             
  92.                 return true;
  93.  
  94.         }//end of switch
  95.  
  96.         return false;
  97.  
  98.     }
  99.  
  100.  
  101. }//end of class